#! /usr/local/bin/python
# -*- coding: utf-8 -*-

'''
模块
'''

import logging, traceback
from tornado.escape import json_encode,json_decode

import log
import util

logger = logging.getLogger(__name__)

 
def method_sys_info(request, data):  
    '''系统广播'''
    socket_id = request.socket_id
    log.print_debug(logger, "method_sys_info exec, socket_id:[%s], data:%s" % (socket_id, data))
    cmd = "sys_info"
    
    data_dict = util.jsonDecode(data)
    tmpReqAction = data_dict.get('type')        #类型
    tmpReqContent = data_dict.get('content')    #内容
    tmpReqContent = '<font color="#FF6600">%s</font>'%(tmpReqContent)

    #"type":
    # 1)  "reload": 重新加载数据库逻辑信息及等级信息
    # 2)  "send":   发送传送过来的 "content"内容 给在线用户
    
    tmpSendMsgUtil = util.SendMsgUtil(request)      #消息发送器
    
    #---重新加载逻辑信息
    if tmpReqAction in ["reload"]:
        util.ConfigManager.reloadConfig()
        tmpSendMsgUtil.send(cmd)
        return 
    
    #---发送消息给所有在线用户
    if tmpReqAction in ["send"]:
        tmpSendMsgUtil.update_item("content", tmpReqContent)
        tmpSendMsgUtil.update_item("type", "sys")    #sys:表示管理员消息
        import user
        user.UserManager.sendMsgToAllExt(cmd, tmpSendMsgUtil)


def method_restart_timer(request, data):  
    '''重启定时器'''
    socket_id = request.socket_id
    log.print_debug(logger, "method_restart_timer exec, socket_id:[%s], data:%s" % (socket_id, data))
    cmd = "restart_timer"
    
    data_dict = util.jsonDecode(data)
    tmpReqType = data_dict.get('type') #类型
    
    import main
    main.startTimer(tmpReqType)
    
    #--回应消息
    tmpSendMsgUtil.send(cmd)
    

#------------------------------------------
#----方法注册---------
util.registMethod("sys_info", method_sys_info)            #系统广播
util.registMethod("restart_timer", method_restart_timer)  #重启定时器
